package com.yimi.yinhepda.views.load_module;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.DepartmentTableDao;
import com.yimi.yinhepda.db.table.DepartmentTable;
import com.yimi.yinhepda.entity.response.QueryWybillExtendVoByWaybillNoArrayResponse;
import com.yimi.yinhepda.entity.response.WaybillExtend;
import com.yimi.yinhepda.event.CountEvent;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.DateHelper;
import com.pda.ym.utils.FastClick;
import com.pda.ym.utils.SoundVibratorManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * 运单开单信息界面
 *
 * @author 000432
 */
public class DetailsActivity extends BaseActivity {
    TextView num, waybill, pickup, line, goodsName, goodsweight, goodsvolume, packing, time;
    TextView receiver_name, receiver_phone, receiver_address, dispath_card, dispath_name,
            dispath_phone, dispath_address;
    TextView collection, insurance_fee, waybill_fee, payment_type, totle;
    TextView dispathCode;
    CheckBox delivery, sign_back;
    String str = "";
    private int operTypeCode;
    private Button bt_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        initview();
        initdata();
        //注册事件
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(CountEvent countEvent) {
        num.setText(countEvent.getCount() + "");
    }

    @Override
    public String getPageName() {
        return "运单开单信息";
    }

    private void initview() {
        ((TextView) findViewById(R.id.title)).setText("运单开单信息");
        num = (TextView) findViewById(R.id.num);

        waybill = (TextView) findViewById(R.id.waybill);
        str = getIntent().getExtras().getString("waybill");
        operTypeCode = getIntent().getExtras().getInt("operTypeCode");

        waybill.setText(str);
        waybill = (TextView) findViewById(R.id.waybill);
        pickup = (TextView) findViewById(R.id.pickup);
        line = (TextView) findViewById(R.id.line);
        goodsName = (TextView) findViewById(R.id.goodsName);
        goodsweight = (TextView) findViewById(R.id.goodsweight);
        goodsvolume = (TextView) findViewById(R.id.goodsvolume);
        packing = (TextView) findViewById(R.id.packing);
        time = (TextView) findViewById(R.id.time);

        receiver_name = (TextView) findViewById(R.id.receiver_name);
        receiver_phone = (TextView) findViewById(R.id.receiver_phone);
        receiver_address = (TextView) findViewById(R.id.receiver_address);
        dispath_card = (TextView) findViewById(R.id.dispath_card);
        dispath_name = (TextView) findViewById(R.id.dispath_name);
        dispath_phone = (TextView) findViewById(R.id.dispath_phone);
        dispath_address = (TextView) findViewById(R.id.dispath_address);

        collection = (TextView) findViewById(R.id.collection);
        insurance_fee = (TextView) findViewById(R.id.insurance_fee);
        waybill_fee = (TextView) findViewById(R.id.waybill_fee);
        payment_type = (TextView) findViewById(R.id.payment_type);
        totle = (TextView) findViewById(R.id.totle);
        dispathCode = (TextView) findViewById(R.id.dispathCode);

        delivery = (CheckBox) findViewById(R.id.delivery);
        sign_back = (CheckBox) findViewById(R.id.sign_back);
        delivery.setClickable(false);
        sign_back.setClickable(false);
        pickup.setVisibility(View.GONE);//隐藏上门提货的布局

        bt_back = (Button) findViewById(R.id.bt_back);
        bt_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (FastClick.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.bt_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void initdata() {
        showPgDlg("正在加载...");

        Map<String, String> params = new HashMap<>();
        params.put("waybillNoArray", str);

        new NetRequest().setMethod(NetRequest.Method.GET)
                .setResponsClazz(QueryWybillExtendVoByWaybillNoArrayResponse.class)
                .setUrl(Constants.URL_GETWAYBILL)
                .setParams(params)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        dismissPgDlg();
                        SoundVibratorManager.playSound(2);
                        showToast("加载数据失败：" + msg);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        if (obj instanceof QueryWybillExtendVoByWaybillNoArrayResponse) {
                            QueryWybillExtendVoByWaybillNoArrayResponse response =
                                    (QueryWybillExtendVoByWaybillNoArrayResponse) obj;
                            dismissPgDlg();
                            //给界面赋值
                            if (response.getData().getWaybillExtendList() != null) {
                                doData(response.getData().getWaybillExtendList().get(0));
                            } else {
                                showToast("无该运单的信息");
                            }
                        }
                    }
                });
    }

    private void doData(WaybillExtend waybillExtend) {
        waybill.setText(waybillExtend.getWaybillNo() + "");
        //根据运单路由查询网点名称
        String routeCode = waybillExtend.getRouteCode();
        if (routeCode != null) {
            DepartmentTableDao departmentTableDao = new DepartmentTableDao();
            StringBuilder sb = new StringBuilder();
            String[] routeCodeArray = routeCode.split("-");
            if (routeCodeArray.length > 0) {
                int startIndex = 0;
                int endIndex = 0;
                for (String item : routeCodeArray) {
                    DepartmentTable departmentTable = departmentTableDao.queryDeptByDeptCode(item);
                    if (departmentTable == null) {
                        continue;
                    }
                    String routeInfo = item + "[" + departmentTable.getDeptName() + "]";
                    String currDeptCode = sharedPreferUtil.getDeptCode();
                    if (currDeptCode.equals(item)) {
                        startIndex = sb.length();
                        endIndex = routeInfo.length();
                    }
                    sb.append(routeInfo);
                    sb.append("-");
                }
                //当前路由设置为绿色字体
                if (sb.length() == 0) {
                    line.setText("");
                    return;
                }
                SpannableString mStyledText = new SpannableString(sb.substring(0, sb.length() - 1));
                mStyledText.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colore1d9717)),
                        startIndex, startIndex + endIndex,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                line.setText(mStyledText);
            } else {
                line.setText("");
            }
        }
        goodsName.setText(waybillExtend.getConsignName());
        goodsweight.setText(waybillExtend.getRealWeight() + "");
        goodsvolume.setText(waybillExtend.getVolume() + "");
        packing.setText(waybillExtend.getPackingSpecification());
        try {
            time.setText(DateHelper.formatTimeEight(waybillExtend.getCreaterTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        receiver_name.setText(waybillExtend.getConsignee());
        receiver_phone.setText(waybillExtend.getConsigneePhone());
        receiver_address.setText(waybillExtend.getConsigneeAddress());
        dispath_card.setText(waybillExtend.getCardNo());
        dispath_name.setText(waybillExtend.getSender());
        dispath_phone.setText(waybillExtend.getSendPhone());
        dispath_address.setText(waybillExtend.getSenderAddress());

        collection.setText(waybillExtend.getCollectionWaybillFee() + "");
        insurance_fee.setText(waybillExtend.getInsuranceAgentFee() + "");
        waybill_fee.setText(waybillExtend.getTotalFreight() + "");
        String paymentType = "";
        //1-寄付 2-到付 3-月结 4-回单付 5-代收扣 6-两边付 7-第三方付)
        if (waybillExtend.getPaymentType().equals("1")) {
            paymentType = "寄付";
        } else if (waybillExtend.getPaymentType().equals("2")) {
            paymentType = "到付";
        } else if (waybillExtend.getPaymentType().equals("3")) {
            paymentType = "月结";
        } else if (waybillExtend.getPaymentType().equals("4")) {
            paymentType = "回单付";
        } else if (waybillExtend.getPaymentType().equals("5")) {
            paymentType = "代收扣";
        } else if (waybillExtend.getPaymentType().equals("6")) {
            paymentType = "两边付";
        } else if (waybillExtend.getPaymentType().equals("7")) {
            paymentType = "第三方付";
        }
        payment_type.setText(paymentType);
        totle.setText(waybillExtend.getTotalFreight() + "");
        if (waybillExtend.getSingBack()) {
            sign_back.setChecked(true);
        }

        if (waybillExtend.getServiceType() == 1) {
            delivery.setChecked(true);
        }
    }
}
